package com.example.limiting.taskmanager2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

public class GetWorkerActivity extends Activity {
    private static final int CONFIRM_RESULT_CODE = 1;
    private static final int CANCEL_RESULT_CODE = 2;
    private static final int DELETE_RESULT_CODE = 3;

    private static final String ADD_TYPE = "add";
    private static final String EDIT_TYPE = "edit";

    private int position;
    private String worker_name;
    private String worker_content;
    private List<String> workers = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_worker);
        Intent intent = getIntent();
        String type = intent.getStringExtra("type");
        String workers_data = intent.getStringExtra("data");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item);

        Log.e("Json",workers_data);
        JSONTokener jsonParser = new JSONTokener(workers_data);

        JSONObject body = null;
        try {
            body = (JSONObject) jsonParser.nextValue();
            Log.e("Json2body",body.toString());
            JSONArray jdata = body.getJSONArray("data");
            Log.e("Json2data",jdata.toString());
            if (jdata.length() == 0){
                Toast.makeText(GetWorkerActivity.this,"你手下没有员工！",Toast.LENGTH_SHORT).show();
                finish();
            }
            for (int i = 0; i < jdata.length(); i++) {
                JSONObject jworker = jdata.getJSONObject(i);
                Log.e("Json2jworker",jworker.toString());
                String workerName = jworker.getString("workerName");
                workers.add(workerName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Spinner spinner = (Spinner) findViewById(R.id.worker_name);
        adapter.addAll(workers);
        spinner.setAdapter(adapter);

        switch (type){
            case ADD_TYPE :
                Button button = (Button) findViewById(R.id.delete_button);
                button.setText("取消");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancelAction(v);
                    }
                });
                break;
            case EDIT_TYPE:
                position = intent.getIntExtra("position",-1);
                worker_name = intent.getStringExtra("worker_name");
                worker_content = intent.getStringExtra("worker_content");
                Spinner workerName = (Spinner) findViewById(R.id.worker_name);
                EditText workerContent = (EditText) findViewById(R.id.worker_content);
                setSpinnerData(workerName,worker_name);
                workerContent.setText(worker_content);
                break;
            default:
                position = intent.getIntExtra("position",-1);
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_get_worker, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void setSpinnerData(Spinner spinner,String firstOne){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        adapter.add(firstOne);
        for (int i = 0; i < workers.size(); i++) {
            if (!firstOne.equals(workers.get(i))){
                adapter.add(workers.get(i));
            }
        }
        spinner.setAdapter(adapter);

    }

    public void deleteAction(View view){
        Intent intent = new Intent();
        intent.putExtra("position",position);
        setResult(DELETE_RESULT_CODE,intent);
        finish();
    }

    public void confirmAction(View view){
        Spinner workerName = (Spinner) findViewById(R.id.worker_name);
        EditText workerContent = (EditText) findViewById(R.id.worker_content);
        Intent intent = new Intent();

        String name = workerName.getAdapter().getItem(workerName.getSelectedItemPosition()).toString();
        intent.putExtra("worker_name", name);
        intent.putExtra("position",position);
        Log.e("spinner text",name);
        intent.putExtra("worker_content",workerContent.getText().toString());
        setResult(CONFIRM_RESULT_CODE,intent);
        finish();
    }

    public void cancelAction(View view){
        setResult(CANCEL_RESULT_CODE);
        finish();
    }


}
